<template>
  <a-form :form="form">
    <div v-for="(item, index) in empArchives.empArchivesList" :key="item.guid || item.id">
      <a-col :span="7" :offset="3">
        <a-form-item label="档案编号">
          <a-input
            v-decorator="[`archivesNumber-${index}`, { initialValue: item.archivesNumber }]"
            :disabled="disable"
            @change="change($event, index, 'archivesNumber')"
          />
        </a-form-item>
      </a-col>
      <a-col :span="7" :offset="3">
        <a-form-item label="档案备注">
          <a-input
            v-decorator="[`archivesRemark-${index}`, { initialValue: item.archivesRemark }]"
            @change="change($event, index, 'archivesRemark')"
            :disabled="disable"
          />
        </a-form-item>
      </a-col>
      <a-col :span="7" :offset="3">
        <a-form-item>
          <a-input v-decorator="[`id-${index}`, { initialValue: String(item.id) }]" type="hidden" />
        </a-form-item>
      </a-col>
      <a-col v-if="!disable" :span="17" :offset="3" class="mb24">
        <a-button type="dashed" style="width:100%;height:44px;" @click="delHandle(item)">删除</a-button>
      </a-col>
      <a-col v-if="index != empArchives.empArchivesList.length - 1" :span="17" :offset="3" class="border-top mb12"></a-col>
    </div>
    <div v-if="!disable">
      <a-col :span="17" :offset="3" class="border-top"></a-col>
      <a-col :span="17" :offset="3" class="mt24 mb24">
        <a-button type="dashed" style="width:100%;height:44px;" @click="addHandle">新增</a-button>
      </a-col>
      <a-col :span="23">
        <div class="basicInfo-button-group">
          <a-button class="mr12" @click="cancel">取消</a-button>
          <a-button type="primary" @click="submit">确认</a-button>
        </div>
      </a-col>
    </div>
  </a-form>
</template>
<script>
import { updateEmpAllInfo } from "@/api/staff/staff";
export default {
  name: "RecordForm",
  props: {
    empArchives: {
      type: Object
    },
    disable: {}
  },
  data() {
    return {
      form: this.$form.createForm(this)
    };
  },

  watch: {
    empArchives(index) {
      if (this.empArchives.empArchivesList == null || this.empArchives.empArchivesList.length === 0) {
        this.$set(this.empArchives, "empArchivesList", [{}]);
      }
    }
  },
  methods: {
    change(e, index, key) {
      this.$set(this.empArchives.empArchivesList, index, {
        ...this.empArchives.empArchivesList[index],
        [key]: e.target.value
      });
    },
    change1(e, index, key) {
      this.$set(this.empArchives.empArchivesList, index, {
        ...this.empArchives.empArchivesList[index],
        [key]: e
      });
    },
    cancel() {
      this.$emit("cancelEdit");
    },
    addHandle() {
      if (this.empArchives.empArchivesList === null) {
        this.empArchives.empArchivesList = [];
      }
      this.empArchives.empArchivesList.push({ guid: Math.random() * 16 });
    },
    delHandle(item) {
      const index = this.empArchives.empArchivesList.indexOf(item);

      if (index !== -1) {
        this.empArchives.empArchivesList.splice(index, 1);
      }
    },
    getMoment(date) {
      return date ? this.moment(date) : null;
    },
    submit() {
      var that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          const value = this.beautyData(values);

          setTimeout(() => {
            updateEmpAllInfo({
              id: this.empArchives.id,
              phone: this.empArchives.phone,
              empArchivesList: value
            }).then(res => {
              that.$notification["success"]({
                message: "success",
                description: "编辑成功"
              });

              that.$emit("cancelEdit");
            });
          }, 300);
        }
      });
    },
    beautyData(data) {
      var arr = [];
      this.empArchives.empArchivesList.forEach((item, index) => {
        arr[index] = {
          archivesNumber: data[`archivesNumber-${index}`],
          archivesRemark: data[`archivesRemark-${index}`],
          id: data[`id-${index}`]
        };
      });
      return arr;
    }
  }
};
</script>

<style scoped>
.basicInfo-button-group {
  text-align: center;
  padding-bottom: 24px;
}
</style>
